# -*- coding: utf-8 -*-
# @Author  : 曾诗诗
# @Time    : 2022/5/3 17:05
# @Function:书籍搜集系统的后端

import sqlite3
import splite_thulac


def connectdb():
    con=sqlite3.connect("../book.db", timeout=10, check_same_thread=False)
    cur=con.cursor()
    cur.execute("select * from catalog")
    rows = cur.fetchall()
    con.close()
    return rows

#根据书名、作者、分类标签和出版社进行精准查找
def accurately_search(rows,searchinfo):
    searchresult=[]
    for row in rows:
        #书名
        if(row[1]!='' and (searchinfo in row[1] or row[1] in searchinfo)):
            searchresult.append(row);
        #作者
        elif(row[3]!='' and (searchinfo in row[3] or row[3] in searchinfo)):
            searchresult.append(row);
        #分类标签
        elif (row[5]!='' and (searchinfo in row[5] or row[5] in searchinfo)):
            searchresult.append(row);
        #出版社
        elif (row[7]!='' and (searchinfo == row[7] or row[7] in searchinfo)):
            searchresult.append(row);
        # 自然语言匹配查找
    searchresult=splite_thulac.natural_search(rows, searchinfo,searchresult)
    return searchresult

# if __name__ == '__main__':
#     searchinfo='讲明朝故事的书'
#     rows=connectdb()
#     accurately_search(rows, searchinfo)



